import { defineStore } from 'pinia';
import { user, vipCode } from '@/services/apis';
import { UserType } from '@/enums/Enum';
import { pageLink } from '../constants/link';
import { convertToUserInfo } from './utils/user';
import { redirect, switchTab } from '/utils/index.ts'
import dayjs from 'dayjs';

// @ts-ignore
export const useUserStore = defineStore('user', {
  state: () => {
    return {
      token: '',
      phone: '',
      userInfo: null,
      isActivateMember: false,
      memberEcpiredDate: ''
    };
  },
  getters: {
    getToken() {
      return this.token;
    },
  },
  actions: {
    setToken(token: any) {
      this.token = token;
    },
    setActivatedMember(isActivateMember: boolean, memberEcpiredDate = '') {
      this.isActivateMember = isActivateMember;
      this.memberEcpiredDate = memberEcpiredDate;
    },
    handleMembershipExpired() {
      this.setActivatedMember(false);
      switchTab(pageLink.index);
    },
    async sendSms(params: any) {
      try {
        await user.sendSms(params);
      } catch (error) {
        throw error;
      }
    },
    async getUserInfo() {
      try {
        const res = await user.getInfo();
        return res;
      } catch (error) {
        return null;
      }
    },
    async updateUserInfo(formInfo) {
      const params = convertToUserInfo(formInfo);
      try {
        const userProfileId = this.userInfo?.userProfileId || '';
        if (userProfileId) {
          await user.updateInfo({ userProfileId, ...params });
        }
        else {
          await user.addInfo(params);
        }
        uni.showToast({
          title: '保存成功',
          icon: 'none',
        });
        const userInfo = await user.getInfo();
        this.userInfo = userInfo;
      } catch (error) {
        throw error;
      }
    },
    async login(params: any) {
      const res = await user.loginV2(params);
      this.setToken(res.token);
      this.phone = params?.phonenumber;
      const userInfo = await user.getInfo();
      this.userInfo = userInfo;
      await this.getVipInfo();
      switchTab(pageLink.index);
    },
    async logout(isTokenInvalid = false) {
      if (!isTokenInvalid) {
        await user.logout();
      }
      this.token = '';
      this.userInfo = null;
      redirect(pageLink.auth);
    },
    async destroy() {
      await user.destroy();
      this.logout(true)
    },
    async useVipCode(params) {
      try {
        const res = await vipCode.useVipCode(params);
        this.setActivatedMember(true, dayjs(res?.expireTime).format('YYYY-MM-DD'));
        return true;
      } catch (error) {
        return false;
      }
    },
    async getVipInfo() {
      try {
        const res = await user.getVipInfo();
        if (res?.isvip === '0') {
          this.handleMembershipExpired()
          return
        }
        this.setActivatedMember(true, dayjs(res?.endTime).format('YYYY-MM-DD'));
      } catch (error) {

      }
    },
    async userReport(params) {
      try {
        const res = await user.userReport(params);
        uni.showToast({ title: '举报成功', icon: 'none' });
      } catch (error) {

      }
    },
    async register(params) {
      try {
        const res = await user.register(params);
        uni.showToast({ title: '注册成功', icon: 'none' });
        redirect(pageLink.auth);
      } catch (error) {

      }
    },
    async getPayInfo(params) {
      try {
        const res = await user.getPayInfo(params);
				console.log("getPayInfo res", res)
      } catch (error) {

      }
    }
  },
  persist: true,
});
